package com.hellogood.yaohuo.client.common;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.entity.Dept;
import com.hellogood.yaohuo.shared.services.DeptService;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;

public class DeptTextItemTreeClickHandler implements KeyPressHandler {
	private TextItem deptItem = null;

	public DeptTextItemTreeClickHandler(TextItem deptItem) {
		this.deptItem = deptItem;
	}

	@Override
	public void onKeyPress(KeyPressEvent event) {
		if (event.getKeyName().equals("Enter")) {
			Object dept = deptItem.getValue();
			if (dept != null && !dept.equals("")) {
				DeptService.Util.getInstance().getDetpByCode(dept.toString(), new AsyncCallback<Dept>() {

					@Override
					public void onFailure(Throwable caught) {
						SC.say(caught.getMessage());
					}

					@Override
					public void onSuccess(Dept result) {
						if (result != null) {
							deptItem.setValue(result.getDepCode() + "-" + result.getDepName());
						} else {
							DeptTreeWindow window = new DeptTreeWindow(deptItem);
							window.setPosition(Positioning.RELATIVE);
							window.setSize("500", "500");
							window.centerInPage();
							window.setTitle("组织选择");
							window.show();
						}
					}
				});
			} else {
				DeptTreeWindow window = new DeptTreeWindow(deptItem);
				window.setPosition(Positioning.RELATIVE);
				window.setSize("500", "500");
				window.centerInPage();
				window.setTitle("组织选择");
				window.show();
			}

		}

	}

}
